// 用户信息接口
//连接用户信息数据库
const userModel = require('../db/users')
// 加密
const bcryptjs = require('bcryptjs')
//令牌
const jwt = require('jsonwebtoken')
const returned = require('../error_returned/returned')
const path = require('path')
module.exports = {
  //用户注册接口
  userregister: (req, res) => {
    // res.send(req.body)
    //获取注册信息
    let { username, password, email } = req.body
    console.log(req.body);
    //查看注册信息是否位空
    if (!username || !password || !email) {
      returned(res, 3, '数据不能有空')
      return
    }
    //查询这是否注册过
    userModel.findOne({ username }, (err, docs) => {
      if (err) {
        returned(res, 2, '注册失败')
        return
      }
      if (docs) {
        returned(res, 3, '用户名被占用')
        return
      }
      //向数据库添加用户信息
      userModel.insertMany({ username, password: bcryptjs.hashSync(password), email }, (err, docs) => {
        if (err) {
          returned(res, 2, '注册失败')
          return
        }
        if (!docs) {
          returned(res, 2, '注册失败')
          return
        }
        returned(res, 0, '注册成功')
      })
    })
  },
  //用户登录接口
  userlogin: (req, res) => {
    // res.send(req.body)
    //获取登录信息
    let { username, password } = req.body
    //查看登录信息是否位空
    if (!username || !password) {
      returned(res, 3, '数据不能有空')
      return
    }
    //查询是否有该账号信息
    userModel.findOne({ username }, (err, docs) => {
      if (err) {
        returned(res, 2, '登录失败')
        return
      }
      if (!docs) {
        returned(res, 3, '账号或密码错误')
        return
      }
      //找到账号后比对密码
      if (!bcryptjs.compareSync(password, docs.password)) {
        returned(res, 3, '账号或密码错误')
        return
      }
      //颁发令牌
      let token = jwt.sign({
        username,
        startTime: +new Date(),
        expires: 7200 * 1000
      }, 'ASDFWETRRTYU$#^5kjfaklsdhfkjashf')
      returned(res, 0, '登录成功', {
        // 颁发令牌 - jsonwebtoken - jwt
        token: token,
        id: docs._id
      })
    })
  },
  //获取用户信息
  userinfo: (req, res) => {
    // res.send(req.params)
    //获取用户名
    let { id } = req.params
    console.log(id);
    //获取令牌
    tokenData = jwt.verify(req.headers.authorization, 'ASDFWETRRTYU$#^5kjfaklsdhfkjashf')
    userModel.findOne({ _id: id }, { __v: 0, password: 0 }, (err, docs) => {
      if (err) {
        returned(res, 2, '获取用户信息失败')
        return
      }
      if (!docs) {
        returned(res, 3, '无该用户')
        return
      }
      // 剔除私密信息
      let { username, email } = docs
      if (tokenData.username != username) {
        returned(res, 6, '不能获取别人的信息')
        return
      }
      docs.avatar = 'http://localhost:8848/' + docs.avatar
      returned(res, 0, '查询成功', docs)
    })
  },
  //修改用户信息
  editUser: async (req, res) => {
    // 从地址栏中获取用户id
    let { _id } = req.params
    // console.log(_id);
    // console.log( req.body );
    let { username, email } = req.body
    // console.log(req.file);
    // 组合文件路径
    // console.log(req.file);
    let filePath = 'uploads/' + req.file.filename
    let docs = await userModel.updateOne({ _id }, {
      username,
      email,
      avatar: filePath
    })
    returned(res, 0, '修改成功')
  },
  //修改密码
  pass: async (req, res) => {
    let { _id, username, password, repass } = req.body
    // 查看有没有该用户
    let flag = await userModel.findOne({ _id })
    console.log(req.body);
    if (!flag) {
      returned(res, 1, '修改失败')
      return
    } else {
      // 验证密码是否正确
      if (bcryptjs.compareSync(password, flag.password)) {
        let docs = await userModel.updateOne({ _id }, {
          password: bcryptjs.hashSync(repass)
        })
        returned(res, 0, '修改成功')
      } else {
        returned(res, 1, '修改失败')
      }
    }
  }
}